//
//  HLShareCollectionView.m
//  YueYue
//
//  Created by hulu001 on 2023/9/21.
//

#import "HLShareCollectionView.h"
#import "HLShareLinkCell.h"
#import "HLSharePosterCell.h"

@interface HLShareCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) QMUIEmptyView *emptyView;
@property (nonatomic, assign) HLShareType type;
@end

@implementation HLShareCollectionView

- (instancetype)initWithType:(HLShareType)type
{
    if (self = [super initWithFrame:CGRectZero]) {
        self.type = type;
        [self setupView];
    }
    return self;
}



- (void)setupView
{
    
    QMUICollectionViewPagingLayout *layout;
    if (self.type == HLShareTypeLink){
        layout = [[QMUICollectionViewPagingLayout alloc] initWithStyle:QMUICollectionViewPagingLayoutStyleScale];
        layout.itemSize = CGSizeMake(356, 106);
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    } else {
        layout = [[QMUICollectionViewPagingLayout alloc] initWithStyle:QMUICollectionViewPagingLayoutStyleScale];
        layout.itemSize = CGSizeMake(256, 356);
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }

    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = UIColorWhite;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    if (self.type == HLShareTypeLink){
        [_collectionView registerClass:[HLShareLinkCell class] forCellWithReuseIdentifier:[HLShareLinkCell identifyCell]];
    } else {
        [_collectionView registerClass:[HLSharePosterCell class] forCellWithReuseIdentifier:[HLSharePosterCell identifyCell]];
    }
    [self addSubview:_collectionView];
    if (self.type == HLShareTypeLink){
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(12);
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
            make.bottom.mas_equalTo(-12);
        }];
    } else {
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(12);
            make.left.mas_equalTo(38);
            make.right.mas_equalTo(-38);
            make.bottom.mas_equalTo(-12);
        }];
    }
    
    
    self.emptyView = [[QMUIEmptyView alloc] init];
    [self.emptyView setImage:UIImageMake(@"empty_icon_6")];
    self.emptyView.imageViewInsets = UIEdgeInsetsMake(0, 0, -26, 0);
    [self.emptyView setTextLabelText:@"空空如也"];
    [self.emptyView setDetailTextLabelText:@"哇哦，这是一个分享也没有"];
    self.emptyView.hidden = YES;
    [self addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-2);
    }];
    
    [self requestShare];
}


- (void)requestShare
{
    HLShareRequest *shareRequest = [[HLShareRequest alloc] initWithType:_type params:@{}];
    [shareRequest startWithCompletionBlockWithSuccess:^(__kindof HLShareRequest * _Nonnull request) {
        if (request.isSuccess){
            self.items = [request itemList];
            HLShareLinkModel *model = [self.items firstObject];
            model.isChange = YES;
            self.changeModel = model;
            [self.collectionView reloadData];
        }
    } failure:nil];
}

- (void)setItems:(NSArray<HLShareLinkModel *> *)items
{
    _items = items;
    self.emptyView.hidden = (items.count > 0);
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell;
    if (self.type == HLShareTypeLink){
        HLShareLinkCell *linkCell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLShareLinkCell identifyCell] forIndexPath:indexPath];
        HLShareLinkModel *model = self.items[indexPath.row];
        linkCell.model = model;
        cell = linkCell;
    } else {
        HLSharePosterCell *posterCell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLSharePosterCell identifyCell] forIndexPath:indexPath];
        posterCell.model = self.items[indexPath.row];
        cell = posterCell;
    }
    
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item >= self.items.count){
        return;
    }
    if (self.changeModel) {
        self.changeModel.isChange = NO;
    }
    HLShareLinkModel *model = self.items[indexPath.row];
    model.isChange = YES;
    self.changeModel = model;
    
    [self.collectionView reloadData];
    
}


- (UIImage *)posterImage
{
    HLSharePosterCell *cell = self.collectionView.visibleCells.firstObject;
   return [cell capture];
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self;
}


@end
